<?php
$action=isset($_GET['action'])?$_GET['action']:null;
$uin=isset($_GET['uin'])?$_GET['uin']:null;
$sig=isset($_GET['sig'])?$_GET['sig']:null;
if($action=='yz'){
	$code=isset($_GET['code'])?$_GET['code']:null;
	$url="http://captcha.qq.com/cap_union_verify?aid=549000929&uin=".$uin."&captype=2&ans=".$code."&sig=".urldecode($_COOKIE['dm_sig'])."&0.29404".time();
	$get=get_curl($url);
	exit($get);
}else{
	if($action=='reload') $sig=urldecode($_COOKIE['dm_sig']);
	$sig=getvc($uin,$sig);
	setcookie('dm_sig',$sig,time()+60*5,'/');
	$url="http://captcha.qq.com/getimgbysig?aid=549000929&uin=".$uin."&sig=".$sig;

	header('content-type:image/jpeg');
	exit(get_curl($url));
}







function getvc($uin,$sig){
	if(strlen($sig)==56){
		$url="http://captcha.qq.com/cap_union_show?captype=3&lang=2052&aid=549000929&uin=".$uin."&cap_cd=".$sig."&v=0.679482".time();
		$data=get_curl($url);
		if(preg_match("/g\_click\_cap\_sig=\"(.*?)\";/", $data, $arr)){
			return $arr[1];
		}
	}else{
		$url="http://captcha.qq.com/getQueSig?aid=549000929&uin=".$uin."&captype=2&sig=".$sig."&v=0.679482".time();
		$data=get_curl($url);
		if(preg_match("/cap_getCapBySig\(\"(.*?)\"\);/", $data, $arr)){
			return $arr[1];
		}
	}
}








function get_curl($url,$post=0,$referer=0,$cookie=0,$header=0,$ua=0,$nobaody=0){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	if($post){
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
	}
	if($header){
		curl_setopt($ch, CURLOPT_HEADER, TRUE);
	}
	if($cookie){
		curl_setopt($ch, CURLOPT_COOKIE, $cookie);
	}
	if($referer){
		if($referer==1){
			curl_setopt($ch, CURLOPT_REFERER, "http://m.qzone.com/infocenter?g_f=");
		}else{
			curl_setopt($ch, CURLOPT_REFERER, $referer);
		}
	}
	if($ua){
		curl_setopt($ch, CURLOPT_USERAGENT,$ua);
	}else{
		curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Linux; U; Android 4.0.4; es-mx; HTC_One_X Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0');
	}
	if($nobaody){
		curl_setopt($ch, CURLOPT_NOBODY,1);
	}
	curl_setopt($ch, CURLOPT_ENCODING, "gzip");
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	$ret = curl_exec($ch);
	curl_close($ch);
	return $ret;
}

	function getcookie($ret,$cookie=array(),$type=0){
		if($type){
			$ret=get_curl($ret,0,0,0,1);
		}
		$ret=str_replace(array('p_uin=;','p_skey=;','pt4_token=;'),array('','',''),$ret);
		if(preg_match_all('/Set\-Cookie\:(\s[a-zA-Z0-9\_\-]+)\=([a-zA-Z0-9\=\_\-\@\:\*]*)\;/iu',$ret,$arr)){
			foreach($arr[1] as $k=>$row){
				$cookie[trim($row)]=$arr[2][$k];
			}
		}
		return $cookie;
	}